<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

final class Produkter extends BIT_Model
{
	public static $class_name = __CLASS__;
	
	protected $id;
	
	protected $typ;
	
	protected $slug;
	
	protected $namn;
	
	protected $beskrivning;
	
	protected $pris;
	
	protected $paket;
	
	protected $erbjudande;
	
	protected $pos;
	
	protected $recept;
	
	protected $art;
	
	protected $fangst;

	protected $hallbarhet;
	
	protected $produktionsmetod;

	protected $image;

	protected $thumb;

	
	private $db = "";
	
	function __construct(){
		parent::__construct();
	}
	
	public function getProductPageProducts($fixpackageprice = false){
		$db = get_instance()->db;

		$products = $db->where('typ !=', 'mix')->order_by('pos' ,'asc')->get('produkter')->result();
		$packages = $db->where('typ', 'mix')->order_by('pos' ,'asc')->get('produkter')->result();
		
		// Fix price based on calculation function
		if( $fixpackageprice ){
			$products = $this->packagePrice($products);
		}

		$products = $this->mergeProdsAndPackagesNicely( $products, $packages, 3 /* boxes in width */ );

		return $products;
	}

	public function getStartpageProducts( $limit = 6, $fixpackageprice = false ){
		$db = get_instance()->db;

		// select two products and one package, merge them (package last) and return
		$products = $db->where('typ !=', 'mix')->order_by('pos' ,'asc')->get('produkter')->result();
		$packages = $db->where('typ', 'mix')->order_by('pos' ,'asc')->get('produkter')->result();

		// Fix price based on calculation function
		if( $fixpackageprice ){
			$products = $this->packagePrice($products);
		}

		// Sort in packages nicely
		//$products = $this->mergeProdsAndPackagesNicely( $products, $packages, 2 /*width*/);
		
		return array_splice($products, 0, $limit);
	}

	private function mergeProdsAndPackagesNicely( $products = array(), $packages = array(), $boxwidthcount = 3){
		$packageRow = true;
		$pos = 0;

		for($i = 0; $i<count($packages);$i++ ){

			$p = $packages[$i];

			if( $packageRow	) {
				$pos += $boxwidthcount;
				$packageRow = false;
			}
			else {
				$pos += ( $boxwidthcount > 2? $boxwidthcount-1 : $boxwidthcount );
				$packageRow = true;
			}
			// put the package in the array
			array_splice($products, $pos, 0, array($p));
			$pos += 1;
		}

		return $products;
	}

	public function getProduct( $id, $fixpackageprice = false ){
		$db = get_instance()->db;
		$products = $db->where('id',$id)->limit(1)->get('produkter')->result();
		
		// Fix price based on calculation function
		if( $fixpackageprice ){
			$products = $this->packagePrice($products);
		}

		return count($products) > 0 ? $products[0] : false;
	}

	public function getBestSellers( $limit = 3, $fixpackageprice = false ){
		$db = get_instance()->db;
		$products = $db->limit( $limit )->order_by('pos')->get('produkter')->result();
		
		// Fix price based on calculation function
		if( $fixpackageprice ){
			$products = $this->packagePrice($products);
		}

		return $products;
	}

	public function getProducts( $fixpackageprice = false ) {
		$db = get_instance()->db;
		$products = $db->get('produkter')->result();

		// Fix price based on calculation function
		if( $fixpackageprice ){
			$products = $this->packagePrice($products);
		}

		return $products;
	}

	// Function to recalculate for offers, rebates etc.

	// THIS SHOULD BE ON THE DATABASE OBJECT !

	private function specialPrices( $prods ){

		// Convert to array if it isn't
		if( ! is_array($prods )) $prods = array($prods);
		
		foreach($prods as $p) {
			// get price offering
			$now = date('Y-m-d H:i:s');
			$offering = $db->
				where('product_id', $p->id)->
				where('starts <', $now)->
				where('expires >', $now)->
				limit(1)->
				get('product_offer')->
				result();
			
			if(count($offering) === 1){
				$p->pris = $offering[0]->newprice;
			}
		}
		// return recalculated price
		return $prods;
	}

	private function packagePrice($prods){
		$db = get_instance()->db;
		
		// single product case
		if(! is_array($prods)) {
			// recalculate price based on package size
			if($prods->typ !== 'mix'){
				$prods->pris = floatval($prods->pris) * floatval($prods->paket);
				$prods->offer_price = floatval($prods->offer_price) * floatval($prods->paket);
			}
			return $prods;
		}
		// array of products' case
		else {

			foreach($prods as $p){
				if($p->typ !== 'mix'){
					// recalculate price based on package size
					$p->pris = floatval($p->pris) * floatval($p->paket);
					$p->offer_price = floatval($p->offer_price) * floatval($p->paket);
				}
			}
			return $prods;
		}
	}

	public function getSliderProducts($limit = 8, $fixpackageprice = false){
		// Save away current timestamp to compare in database
		$now = date("Y-m-d H:i:s");

		$db = get_instance()->db;
		$products = $db
			->get_where('produkter', 
						array(
							'offer_expires >='=>$now,
							'in_slider'=>true
						),
						$limit)
			->result();	

		// Fix price based on calculation function
		if( $fixpackageprice ){
			$products = $this->packagePrice($products);
		}

		return $products;
	}
		
	public function saveProduct($data){
		
		if(!$data){
			return false;
		}

		foreach($data as $key => $value){
			$this->$key = $value;
		}
		
		$this->save();
		
		return true;
	}
}